package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.Ignore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "02.车辆管理")
@Slf4j
@RestController
@RequestMapping("/v1/vehicle/")
@Validated
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;
    @PostMapping("add")
    @ApiOperation("添加车辆功能")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session){
        log.debug("vehicleAddParam = "+vehicleAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if(userVO == null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }   登录状态校验拦截器
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setUpdateTime(new Date());
        vehicle.setCreateTime(new Date());
        vehicle.setUserId(userVO.getId());
        vehicleMapper.addInsertVehicle(vehicle);
        return JsonResult.ok(vehicle);

    }
    @GetMapping("list")
    @ApiOperation("查询车辆功能")
    @ApiOperationSupport(order = 20)
    public JsonResult vehicleList(VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session){
        log.debug("vehicleListQuery = "+vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        List<VehicleListVO> vehicleList = vehicleMapper.vehicleList(vehicleListQuery);
        return JsonResult.ok(vehicleList);
    }
    @PostMapping("del/{id}")
    @ApiOperation("删除车辆功能")
    @ApiOperationSupport(order = 30)
    public JsonResult delVehicle(@Range(max = 100,min = 1 ,message = "id必须在1~100之间") @PathVariable Long id, @ApiIgnore HttpSession session){
        //手动抛出异常
        if(id < 0){
            throw  new IllegalArgumentException("id值非法");
        }

        log.debug("{id} = "+id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Long userId = userVO.getId();
        int i = vehicleMapper.delIdvehicle(id, userId);
        if(i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.NOT_IllEGAL_USER);
    }

}
